// Copyright 2023 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

// This file generates descriptor.ts. Run itself:
//go:generate go run gen.go

import (
	"fmt"
	"io"
	"os"
	"os/exec"

	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/descriptorpb"
)

func run() error {
	descFile, err := os.CreateTemp("", "desc")
	if err != nil {
		return err
	}

	protoc := exec.Command(
		"protoc",
		"--descriptor_set_out="+descFile.Name(),
		"--include_imports",
		"--include_source_info",
		"testdata/types.proto")
	protoc.Stdout = os.Stdout
	protoc.Stderr = os.Stderr
	if err := protoc.Run(); err != nil {
		return fmt.Errorf("protoc: %s", err)
	}

	descBytes, err := io.ReadAll(descFile)
	if err != nil {
		return err
	}
	if len(descBytes) == 0 {
		return fmt.Errorf("desc file was read as empty")
	}

	desc := &descriptorpb.FileDescriptorSet{}
	if err := proto.Unmarshal(descBytes, desc); err != nil {
		return fmt.Errorf("could not read descriptor file: %s", err)
	}

	m := protojson.MarshalOptions{Multiline: true, Indent: "  "}
	blob, err := m.Marshal(desc)
	if err != nil {
		return err
	}

	out, err := os.Create("testdata/descriptor.ts")
	if err != nil {
		return err
	}
	_, err = fmt.Fprintf(out, `// Copyright 2023 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file is generated by gen.go.
// To update run "go generate .".

export const TestDescriptor = %s;

export default TestDescriptor;
`, blob)
	return err
}

func main() {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
